package com.alex.ioc.properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Application {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("properties.xml");

        DataSource dataSource1 = (DataSource) ctx.getBean("dataSource1");
        DataSource dataSource2 = (DataSource) ctx.getBean("dataSource2");

        // 原生java方式创建连接池
        ComboPooledDataSource dataSource3 = new ComboPooledDataSource();
        System.out.println(dataSource3);

//        Connection connection = C3P0Util.getConnection();

        try {
            Connection conn = dataSource3.getConnection();
//            System.out.println(conn);
            String sql = "select * from user where id=1";
            Statement stmt = conn.createStatement();
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                String username = resultSet.getString("username");
                System.out.println(username);
            }
            resultSet.close();
            stmt.close();
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
